<template>
	<view class="">
		<view class="search-box">
			<u-search placeholder="请输入项目名称" shape="square" v-model="searchData.name" @search="onSearch" :showAction="false" />
			<view class="custom-btn" @click="$u.route('/pages/project/project/edit')" v-if="userInfo.roleCategoryId!==2">新增</view>
		</view>
		<view class="my-list">
			<u-list :height="swiperHeight" @scrolltolower="scrolltolower" v-if="dataList.length > 0">
				<u-list-item v-for="(proIm, proIx) in userInfo.roleCategoryId!==2?dataList:dataList.filter(im=>im.onLine)" :key="proIm.id">
					<view class="item fsize-26 flex align-center" :class="!proIm.onLine?'bg-grey-light':''"
						@click="onClickRow(proIx)">
						<image class="width-140 height-140 border-radius-10 shrink0" :src="proIm.image"></image>
						<view class="w-100 ml-20">
							<view class="">{{proIm.name}}</view>
							<view class="mt-10 text-gray fsize-26">预约量{{proIm.subscribeCount}}</view>
							<view class="mt-10 fsize-36 text-red"><text class="fsize-24">￥</text>{{proIm.price}}</view>
						</view>
						<view class="width-140 shrink0" v-if="userInfo.roleCategoryId ===2">
							<u-button color="#e54d42" text="分享" shape="circle" @click="onShare(proIm.id, proIm.shareCode, 15)"></u-button>
						</view>
					</view>
				</u-list-item>
				<u-loadmore 
					:status="loadmore.status" 
					:icon-type="loadmore.iconType"
					:load-text="loadmore.loadText.loadText" 
					margin-top="20" 
					margin-bottom="20"
					class="coreshop-padding-top-10" 
				/>
			</u-list>
		</view>
		<!-- 无数据时默认显示 -->
		<view class="emptybox" v-if="dataList.length == 0">
			<u-empty icon-size="150" text="暂无数据" mode="data"></u-empty>
		</view>
		<!-- 操作菜单 -->
		<u-action-sheet 
			:actions="actionSheet.list" 
			@select="selectClick" 
			:title="actionSheet.title"
			:show="actionSheet.show" 
			:closeOnClickAction="true" 
			cancelText="取消" 
			@close="actionSheet.show=false"
		>
		</u-action-sheet>
	</view>
</template>

<script>
	import listHeightMixin from '@/common/computedAltitude.js'
	import share from '@/common/mixins/share.js'
	import { mapState } from 'vuex'
	export default {
		mixins:[listHeightMixin,share],
		data() {
			return {
				loadmore: {
					status: 'loadmore',
					iconType: 'flower',
					loadText: {
						loadmore: '轻轻上拉',
						loading: '努力加载中',
						nomore: '实在没有了'
					},
				},
				curNow: 0,
				dataList: [],
				searchData: {
					name: '',
					page: 1,
					limit: 99,
					share: 1,
				},
				actionSheet: {
					show: false,
					title: '',
					list: []
				}
			}
		},
		onLoad() {
			this.getPageList()
			uni.$on('upprolist', ()=>{
				this.onSearch()
			})
		},
		onUnload() {
			uni.$off('upprolist')
		},
		computed: {
			...mapState(['userInfo'])
		},
		methods: {
			selectClick(index) {
				let that = this
				let ix = index.ix
				if(index.name==='删除'){
					uni.showModal({
						title: '提示',
						content: `确定要删除${that.dataList[ix].name}吗?`,
						success: function (res) {
							if (res.confirm) {
								that.$reqJsonPost('CoreCmsProjectDoDelete',{ id:index.id }).then(res=>{
									if(res.code === 0){
										that.dataList.splice(ix, 1)
									}
								})
							}
						}
					})
				}else if(index.name==='下架'){
					this.$reqJsonPost('CoreCmsProjectDoSetonLine',{
						data: false,
						id: this.dataList[ix].id
					}).then(res=>{
						if(res.code === 0){
							this.dataList[ix].onLine = false
						}
					})
				}else if(index.name==='上架'){
					this.$reqJsonPost('CoreCmsProjectDoSetonLine',{
						data: true,
						id: this.dataList[ix].id
					}).then(res=>{
						if(res.code === 0){
							this.dataList[ix].onLine = true
						}
					})
				}else if(index.name==='编辑'){
					this.$u.route('/pages/project/project/edit',{ id:index.id })
				}
			},
			onClickRow(index) {
				if(this.userInfo.roleCategoryId === 2) return 
				let item = this.dataList[index]
				this.actionSheet.list = [{
					name: '删除',
					id: item.id,
					ix:index
				}, {
					name: item.onLine ? '下架' : '上架',
					id: item.id,
					ix:index
				}, {
					name: '编辑',
					id: item.id,
					ix:index
				}]
				this.actionSheet.title = item.name
				this.actionSheet.show = true
			},
			// 搜索事件
			onSearch() {
				this.searchData.page = 1
				this.dataList = []
				this.getPageList()
			},
			getPageList() {
				this.$reqPost('CoreCmsProjectList', this.searchData).then(res => {
					this.dataList = [...this.dataList, ...res.data]
					if (res.count > this.dataList.length) {
						this.loadmore.status = 'loadmore'
						this.searchData.page++
					} else {
						// 数据已加载完毕
						this.loadmore.status = 'nomore'
					}
				})
			},
			scrolltolower() {
				if (this.loadmore.status === 'loadmore') {
					this.getPageList()
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.img {
		width: 140rpx;
		height: 140rpx;
		border-radius: 10rpx;
	}
</style>
